<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="layui/css/layui.css"  media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
 <form class="layui-form" >
<table class="layui-hide" id="test"></table>
<div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
    </div>
  </div>
</form>
<script type="text/html" id="switchTpl">
  <!-- 这里的 checked 的状态只是演示 -->
  <input type="checkbox" name="sex" value="{{d.sex}}" lay-skin="switch" lay-text="女|男" lay-filter="sexDemo" {{ d.sex == '女' ? 'checked' : '' }}>
</script>
 
<script type="text/html" id="checkboxTpl">
  <!-- 这里的 checked 的状态只是演示 -->
  <input type="checkbox" name="lock" value="{{d.id}}" title="锁定" lay-filter="lockDemo" {{ d.id == 10006 ? 'checked' : '' }}>
</script>

              
          
<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 
 
<script>
layui.use('table', function(){
  var table = layui.table
  ,form = layui.form;
  var $ = layui.$;
  
  table.render({
    elem: '#test'
    ,id:'test'
    ,url:'data.json'
    ,cellMinWidth: 80
    ,cols: [[
      {type:'numbers'}
      ,{type: 'checkbox',LAY_CHECKED:true,hide:true}
      ,{field:'id', title:'ID', width:100, unresize: true, sort: true}
      ,{field:'username',edit:'text', title:'用户名'}
      ,{field:'city',edit:'text', title:'城市'}
      ,{field:'wealth',edit:'text', title: '财富', minWidth:120, sort: true}
      ,{field:'sex',title:'性别', width:85, templet: '#switchTpl', unresize: true}
      ,{field:'lock', title:'是否锁定', width:110, templet: '#checkboxTpl', unresize: true}
    ]]
    ,page: true
  });
  
  //监听性别操作
  form.on('switch(sexDemo)', function(obj){
    form.render('checkbox');
    if(obj.elem.checked){
        obj.value="女";
    }else{
        obj.value="男";
    }
    console.log(obj);
    var index = $(this).parents("tr[data-index]").attr('data-index');
    console.log(index);
    table.cache.test[index].sex=obj.value;
  });
  
  //监听锁定操作
  form.on('checkbox(lockDemo)', function(obj){
    console.log(obj);
  });

//监听提交
    form.on('submit(demo1)', function(data){
        var checkStatus = table.checkStatus('test');
        console.log(table.cache);
        console.log(checkStatus);
        console.log(data);
        return false;
    });

});
</script>

</body>
</html>